package com.dianban.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.dianban.base.ResultCode;
import com.dianban.base.ResultVo;
import com.dianban.config.redis.RedisUtil;
import com.dianban.dao.FacePayCommissionLogDao;
import com.dianban.entity.*;
import com.dianban.service.*;
import io.swagger.models.auth.In;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.applet.Main;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * 用户业务实现层
 *
 * Created by bysocket on 07/02/2017.
 */
@Service
@Transactional(readOnly = true)
public class FacePayCommissionLogServiceImpl implements FacePayCommissionLogService {

    @Autowired
    private FacePayCommissionLogDao facePayCommissionLogDao;
    @Autowired
    private ShoproOrderService shoproOrderService;
    @Autowired
    private UserService userService;
    @Autowired
    private ShopUserService shopUserService;
    @Autowired
    private StorePrincipalService storePrincipalService;
    @Autowired
    private StoreManagerService storeManagerService;
    @Autowired
    private FacePayStoreOrderService facePayStoreOrderService;
    @Autowired
    private StoreGuideService storeGuideService;
    @Autowired
    private FacePayCommissionConfigService facePayCommissionConfigService;

    @Override
    @Transactional(readOnly = false)
    public int insert(FacePayCommissionLog facePayCommissionLog) {
        facePayCommissionLog.setCreatetime(System.currentTimeMillis()/1000);
        return facePayCommissionLogDao.insert(facePayCommissionLog);
    }

    @Override
    @Transactional(readOnly = false)
    public void commission(FacePayStoreConsumeLog facePayStoreConsumeLog,Long shopUserId) {
        User user = null;
        BigDecimal money = null;
        ShopUser shopUser = null;
        if( facePayStoreConsumeLog.getOnlineOrderId() != null){
            ShoproOrder shoproOrder = shoproOrderService.findById(facePayStoreConsumeLog.getOnlineOrderId());
            BigDecimal orderMoney = shoproOrder.getTotalAmount();
            // 查询订单所属店铺 没有配置拿当前店铺
            Long storeId = shoproOrder.getStoreId();
            if(storeId == null){
                storeId = facePayStoreConsumeLog.getStoreId();
            }
            //分佣配置人员
            if(orderMoney != null && storeId != null){
                List<FacePayCommissionConfig> facePayCommissionConfigList = facePayCommissionConfigService.findList(storeId,  "10",orderMoney);
                for(FacePayCommissionConfig facePayCommissionConfig : facePayCommissionConfigList){
                    Long userId = facePayCommissionConfig.getUserId();
                    BigDecimal commissionMoney =  facePayCommissionConfig.getCommissionMoney();
                    BigDecimal commissionRatio = facePayCommissionConfig.getCommissionRatio();
                    String commissionName = facePayCommissionConfig.getCommissionName();
                    String commissionType = facePayCommissionConfig.getCommissionType();
                    money = commissionMoney.multiply(commissionRatio).divide(new BigDecimal(100),3,RoundingMode.DOWN);
                    // 10 店老板 11 能量舱合伙人 12 空间站站长  13 美导 14 美导顾客佣金 15项目店长  99 其他
                    if(StringUtils.isNotBlank(commissionType) && commissionType.equals("10")){
                        if(userId == null){
                            userId = shopUserId;
                        }
                        userService.updateFacePayCommission(money,userId,facePayStoreConsumeLog.getType(),facePayStoreConsumeLog.getOnlineOrderId(),commissionName,facePayCommissionConfig);
                    }else if(StringUtils.isNotBlank(commissionType) && commissionType.equals("11")){
                        // 10人分享佣金
                        money = commissionMoney.multiply(commissionRatio).divide(new BigDecimal(1000),3,RoundingMode.DOWN);
                        Set<Long> partnerUserId = getPartnerUserId(storeId);
                        for (Long userId1 : partnerUserId) {
                            userService.updateFacePayCommission(money, userId1, facePayStoreConsumeLog.getType(), facePayStoreConsumeLog.getOnlineOrderId(),commissionName,facePayCommissionConfig);
                        }
                    }else if(StringUtils.isNotBlank(commissionType) && commissionType.equals("12")){
                        if(userId != null){
                            userService.updateFacePayCommission(money,userId,facePayStoreConsumeLog.getType(),facePayStoreConsumeLog.getOnlineOrderId(),commissionName,facePayCommissionConfig);
                        }else{
                            StorePrincipal storePrincipal = storePrincipalService.findByStoreId(storeId);
                            if(storePrincipal != null && storePrincipal.getUserId() != null){
                                userService.updateFacePayCommission(money, storePrincipal.getUserId(), facePayStoreConsumeLog.getType(), facePayStoreConsumeLog.getOnlineOrderId(),commissionName,facePayCommissionConfig);
                            }
                        }
                    }else if(StringUtils.isNotBlank(commissionType) && commissionType.equals("15")){
                        if(userId != null){
                            userService.updateFacePayCommission(money,userId,facePayStoreConsumeLog.getType(),facePayStoreConsumeLog.getOnlineOrderId(),commissionName,facePayCommissionConfig);
                        }else{
                            StoreManager storeManager = storeManagerService.findByStoreId(storeId);
                            if(storeManager != null && storeManager.getUserId() != null){
                                userService.updateFacePayCommission(money, storeManager.getUserId(), facePayStoreConsumeLog.getType(), facePayStoreConsumeLog.getOnlineOrderId(),commissionName,facePayCommissionConfig);
                            }
                        }
                    }else{
                        if(userId != null){
                            userService.updateFacePayCommission(money,userId,facePayStoreConsumeLog.getType(),facePayStoreConsumeLog.getOnlineOrderId(),commissionName,facePayCommissionConfig);
                        }
                    }
                }
            }
           /* // 店老板
            money = new BigDecimal("50");
            userService.updateFacePayCommission(money,shopUserId,facePayStoreConsumeLog.getType(),facePayStoreConsumeLog.getOnlineOrderId(),"店老板");
            if(storeId != null){
                // 能量舱合伙人
                Set<Long> partnerUserId = getPartnerUserId(storeId);
                for (Long userId1 : partnerUserId) {
                    money = new BigDecimal("3");
                    userService.updateFacePayCommission(money, userId1, facePayStoreConsumeLog.getType(), facePayStoreConsumeLog.getOnlineOrderId(),"能量舱合伙人");
                }
                // 站长
                StorePrincipal storePrincipal = storePrincipalService.findByStoreId(storeId);
                if(storePrincipal != null && storePrincipal.getUserId() != null){
                    money = new BigDecimal("10");
                    userService.updateFacePayCommission(money, storePrincipal.getUserId(), facePayStoreConsumeLog.getType(), facePayStoreConsumeLog.getOnlineOrderId(),"站长");
                }
            }
            // 订单用户
            shopUser = shopUserService.findById(shoproOrder.getUserId());
            // 其他固定人员
            JSONObject fixedCommission = null;
            if(StringUtils.isNotBlank(shopUser.getOnlineCommissionRatio())){
                fixedCommission = JSONObject.parseObject(shopUser.getOnlineCommissionRatio());
                String devicePartner = (String) fixedCommission.get("devicePartner");
                if(StringUtils.isNotBlank(devicePartner)){
                    money = new BigDecimal("5");
                    userService.updateFacePayCommission(money, Long.parseLong(devicePartner), facePayStoreConsumeLog.getType(), facePayStoreConsumeLog.getOnlineOrderId(),"设备合伙人");
                }
                String provinceAgency = (String) fixedCommission.get("provinceAgency");
                if(StringUtils.isNotBlank(provinceAgency)){
                    money = new BigDecimal("1");
                    userService.updateFacePayCommission(money, Long.parseLong(provinceAgency), facePayStoreConsumeLog.getType(), facePayStoreConsumeLog.getOnlineOrderId(),"省代");
                }
                String cityAgency = (String) fixedCommission.get("cityAgency");
                if(StringUtils.isNotBlank(cityAgency)){
                    money = new BigDecimal("1");
                    userService.updateFacePayCommission(money, Long.parseLong(cityAgency), facePayStoreConsumeLog.getType(), facePayStoreConsumeLog.getOnlineOrderId(),"市代");
                }
                String areaAgency = (String) fixedCommission.get("areaAgency");
                if(StringUtils.isNotBlank(areaAgency)){
                    money = new BigDecimal("1");
                    userService.updateFacePayCommission(money, Long.parseLong(areaAgency), facePayStoreConsumeLog.getType(), facePayStoreConsumeLog.getOnlineOrderId(),"区（县）代");
                }
                String marketOperationCompany = (String) fixedCommission.get("marketOperationCompany");
                if(StringUtils.isNotBlank(marketOperationCompany)){
                    money = new BigDecimal("1");
                    userService.updateFacePayCommission(money, Long.parseLong(marketOperationCompany), facePayStoreConsumeLog.getType(), facePayStoreConsumeLog.getOnlineOrderId(),"市场运营公司");
                }
                String attractExecuteCompany = (String) fixedCommission.get("attractExecuteCompany");
                if(StringUtils.isNotBlank(attractExecuteCompany)){
                    money = new BigDecimal("1");
                    userService.updateFacePayCommission(money, Long.parseLong(attractExecuteCompany), facePayStoreConsumeLog.getType(), facePayStoreConsumeLog.getOnlineOrderId(),"招商执行公司");
                }
            }*/
        }
    }

    @Override
    @Transactional(readOnly = false)
    public void commission (FacePayStoreOrder facePayStoreOrder){
        Long orderId = facePayStoreOrder.getId();
        if(facePayStoreOrder.getPayStatus() != 20 && facePayStoreOrder.getCommissionStatus() != 10){
            return;
        }
        String key = "commissionFacePayStoreOrder:"+orderId;
        if(RedisUtil.hasKey(key)){
            return;
        }
        RedisUtil.set(key,1);
        BigDecimal money = null;
        Long storeId = facePayStoreOrder.getStoreId();
        BigDecimal orderMoney = facePayStoreOrder.getMoney();
        //分佣配置人员
        if(orderMoney != null && storeId != null){
            List<FacePayCommissionConfig> facePayCommissionConfigList = facePayCommissionConfigService.findList(storeId,  "20",orderMoney);
            for(FacePayCommissionConfig facePayCommissionConfig : facePayCommissionConfigList){
                Long userId = facePayCommissionConfig.getUserId();
                BigDecimal commissionMoney =  facePayCommissionConfig.getCommissionMoney();
                BigDecimal commissionRatio = facePayCommissionConfig.getCommissionRatio();
                String commissionName = facePayCommissionConfig.getCommissionName();
                String commissionType = facePayCommissionConfig.getCommissionType();
                money = commissionMoney.multiply(commissionRatio).divide(new BigDecimal(100),3,RoundingMode.DOWN);
                // 10 店老板 11 能量舱合伙人 12 空间站站长  13 美导 14 美导顾客佣金 15项目店长  99 其他
                if(StringUtils.isNotBlank(commissionType) && commissionType.equals("10")){
                    if(userId == null){
                        userId = facePayStoreOrder.getStoreManagerId();
                    }
                    if(userId != null){
                        userService.updateFacePayCommission(money, userId, "20", orderId,commissionName,facePayCommissionConfig);
                    }
                }else if(StringUtils.isNotBlank(commissionType) && commissionType.equals("11")){
                    // 10人分享佣金
                    money = commissionMoney.multiply(commissionRatio).divide(new BigDecimal(1000),3,RoundingMode.DOWN);
                    Set<Long> partnerUserId = getPartnerUserId(storeId);
                    for (Long userId1 : partnerUserId) {
                        userService.updateFacePayCommission(money, userId1, "20", orderId,commissionName,facePayCommissionConfig);
                    }
                }else if(StringUtils.isNotBlank(commissionType) && commissionType.equals("12")){
                    if(userId != null){
                        userService.updateFacePayCommission(money, userId, "20", orderId,commissionName,facePayCommissionConfig);
                    }else{
                        StorePrincipal storePrincipal = storePrincipalService.findByStoreId(storeId);
                        if(storePrincipal != null && storePrincipal.getUserId() != null){
                            userService.updateFacePayCommission(money, storePrincipal.getUserId(), "20", orderId,commissionName,facePayCommissionConfig);
                        }
                    }
                }else if(StringUtils.isNotBlank(commissionType) && commissionType.equals("13")){
                    if(userId == null){
                        userId = facePayStoreOrder.getGuideUserId();
                    }
                    if(userId != null){
                        Long guideUserPid = null;
                        User user = userService.findById(facePayStoreOrder.getUserId());
                        if(user.getPid() != null){
                            StoreGuide storeGuide = storeGuideService.findByUserId(user.getPid());
                            if(storeGuide != null){
                                guideUserPid = storeGuide.getUserId();
                            }
                        }
                        if(guideUserPid != null){
                            FacePayCommissionConfig facePayCommissionConfig14 = null;
                            for(FacePayCommissionConfig facePayCommissionConfig1 : facePayCommissionConfigList){
                                if(StringUtils.isNotBlank(commissionType) && commissionType.equals("14")){
                                    facePayCommissionConfig14 = facePayCommissionConfig1;
                                }
                            }
                            if(facePayCommissionConfig14 != null){
                                commissionRatio = commissionRatio.subtract(facePayCommissionConfig14.getCommissionRatio());
                                money = facePayCommissionConfig14.getCommissionMoney().multiply(facePayCommissionConfig14.getCommissionRatio()).divide(new BigDecimal(100),3,RoundingMode.DOWN);
                                userService.updateFacePayCommission(money, guideUserPid, "20", orderId,facePayCommissionConfig14.getCommissionName(),facePayCommissionConfig14);
                            }
                        }
                        money = commissionMoney.multiply(commissionRatio).divide(new BigDecimal(100),3,RoundingMode.DOWN);
                        userService.updateFacePayCommission(money, userId, "20", orderId,commissionName,facePayCommissionConfig);
                    }
                }else if(StringUtils.isNotBlank(commissionType) && commissionType.equals("15")){
                    if(userId != null){
                        userService.updateFacePayCommission(money, userId, "20", orderId,commissionName,facePayCommissionConfig);
                    }else{
                        StoreManager storeManager = storeManagerService.findByStoreId(storeId);
                        if(storeManager != null && storeManager.getUserId() != null){
                            userService.updateFacePayCommission(money, storeManager.getUserId(), "20", orderId,commissionName,facePayCommissionConfig);
                        }
                    }
                }else{
                    if(userId != null && !"14".equals(commissionType)){
                        userService.updateFacePayCommission(money, userId, "20", orderId,commissionName,facePayCommissionConfig);
                    }
                }
            }
        }
        /*   Long userId = facePayStoreOrder.getUserId();
        Long guideUserId = facePayStoreOrder.getGuideUserId();
        Long guideUserPid = null;
        if(guideUserId != null){
            User user = userService.findById(userId);
            if(user.getPid() != null){
                StoreGuide storeGuide = storeGuideService.findByUserId(user.getPid());
                if(storeGuide != null){
                    guideUserPid = storeGuide.getUserId();
                }
            }
        }
        ShopUser shopUser = shopUserService.findByStoreId(storeId);
        if(facePayStoreOrder.getMoney().compareTo(new BigDecimal("5000")) == 0
                && facePayStoreOrder.getPayStatus() == 20
                && facePayStoreOrder.getCommissionStatus() == 10 ){
            // 门店老板
            money = new BigDecimal("3500");
            userService.updateFacePayCommission(money, facePayStoreOrder.getStoreManagerId(), "20", orderId,"门店老板");
            // 能量舱合伙人
            Set<Long> partnerUserId = getPartnerUserId(storeId);
            for (Long userId1 : partnerUserId) {
                money = new BigDecimal("15");
                userService.updateFacePayCommission(money, userId1, "20", orderId,"能量舱合伙人");
            }
            // 站长
            StorePrincipal storePrincipal = storePrincipalService.findByStoreId(storeId);
            if(storePrincipal != null && storePrincipal.getUserId() != null){
                money = new BigDecimal("100");
                userService.updateFacePayCommission(money, storePrincipal.getUserId(), "20", orderId,"亚健康空间站资源方（站长）");
            }
            // 卖手
            if(guideUserId != null){
                if(guideUserPid != null){
                    money = new BigDecimal("150");
                    userService.updateFacePayCommission(money, guideUserPid, "20", orderId,"美导顾客佣金");
                    money = new BigDecimal("350");
                    userService.updateFacePayCommission(money, guideUserId, "20", orderId,"亚健康护理美导师（卖手）");
                }else{
                    money = new BigDecimal("500");
                    userService.updateFacePayCommission(money, guideUserId, "20", orderId,"亚健康护理美导师（卖手）");
                }
            }
            // 其他固定人员
            JSONObject fixedCommission = null;
            if(StringUtils.isNotBlank(shopUser.getOfflineCommissionRatio())){
                fixedCommission = JSONObject.parseObject(shopUser.getOfflineCommissionRatio());
                String platform = (String) fixedCommission.get("platform");
                if(StringUtils.isNotBlank(platform)){
                    money = new BigDecimal("50");
                    userService.updateFacePayCommission(money, Long.parseLong(platform), "20", orderId,"荣耀兔（平台）");
                }
                String technology = (String) fixedCommission.get("technology");
                if(StringUtils.isNotBlank(technology)){
                    money = new BigDecimal("50");
                    userService.updateFacePayCommission(money, Long.parseLong(technology), "20", orderId,"荣耀兔（技术）");
                }
                String devicePartner = (String) fixedCommission.get("devicePartner");
                if(StringUtils.isNotBlank(devicePartner)){
                    money = new BigDecimal("250");
                    userService.updateFacePayCommission(money, Long.parseLong(devicePartner), "20", orderId,"设备合伙人");
                }
                String projectOperationCompanyBase = (String) fixedCommission.get("projectOperationCompanyBase");
                if(StringUtils.isNotBlank(projectOperationCompanyBase)){
                    money = new BigDecimal("100");
                    userService.updateFacePayCommission(money, Long.parseLong(projectOperationCompanyBase), "20", orderId,"项目承载运营公司（基础保障费用）");
                }
                String healthSeller = (String) fixedCommission.get("healthSeller");
                if(guideUserId == null && StringUtils.isNotBlank(healthSeller)){
                    money = new BigDecimal("500");
                    userService.updateFacePayCommission(money, Long.parseLong(healthSeller), "20", orderId,"亚健康护理美导师（卖手）");
                }
                String marketOperationCompany = (String) fixedCommission.get("marketOperationCompany");
                if(StringUtils.isNotBlank(marketOperationCompany)){
                    money = new BigDecimal("50");
                    userService.updateFacePayCommission(money, Long.parseLong(marketOperationCompany), "20", orderId,"市场运营公司");
                }
                String attractExecuteCompany = (String) fixedCommission.get("attractExecuteCompany");
                if(StringUtils.isNotBlank(attractExecuteCompany)){
                    money = new BigDecimal("50");
                    userService.updateFacePayCommission(money, Long.parseLong(attractExecuteCompany), "20", orderId,"招商执行公司");
                }
                String projectOperationCompany = (String) fixedCommission.get("projectOperationCompany");
                if(StringUtils.isNotBlank(projectOperationCompany)){
                    money = new BigDecimal("50");
                    userService.updateFacePayCommission(money, Long.parseLong(projectOperationCompany), "20", orderId,"项目承载运营公司");
                }
                String provinceAgency = (String) fixedCommission.get("provinceAgency");
                if(StringUtils.isNotBlank(provinceAgency)){
                    money = new BigDecimal("50");
                    userService.updateFacePayCommission(money, Long.parseLong(provinceAgency),"20", orderId,"省代");
                }
                String cityAgency = (String) fixedCommission.get("cityAgency");
                if(StringUtils.isNotBlank(cityAgency)){
                    money = new BigDecimal("50");
                    userService.updateFacePayCommission(money, Long.parseLong(cityAgency), "20", orderId,"市代");
                }
                String areaAgency = (String) fixedCommission.get("areaAgency");
                if(StringUtils.isNotBlank(areaAgency)){
                    money = new BigDecimal("50");
                    userService.updateFacePayCommission(money, Long.parseLong(areaAgency), "20", orderId,"区（县）代");
                }
            }
        }else if(facePayStoreOrder.getMoney().compareTo(new BigDecimal("3999")) == 0
                && facePayStoreOrder.getPayStatus() == 20
                && facePayStoreOrder.getCommissionStatus() == 10 ){
            // 门店老板
            money = new BigDecimal("1999.5");
            userService.updateFacePayCommission(money, facePayStoreOrder.getStoreManagerId(), "20", orderId,"门店老板");
            // 能量舱合伙人
            Set<Long> partnerUserId = getPartnerUserId(storeId);
            for (Long userId1 : partnerUserId) {
                money = new BigDecimal("11.997");
                userService.updateFacePayCommission(money, userId1, "20", orderId,"能量舱合伙人");
            }
            // 站长
            StorePrincipal storePrincipal = storePrincipalService.findByStoreId(storeId);
            if(storePrincipal != null && storePrincipal.getUserId() != null){
                money = new BigDecimal("79.98");
                userService.updateFacePayCommission(money, storePrincipal.getUserId(), "20", orderId,"亚健康空间站资源方（站长）");
            }
            // 卖手
            if(guideUserId != null){
                if(guideUserPid != null){
                    money = new BigDecimal("119.97");
                    userService.updateFacePayCommission(money, guideUserPid, "20", orderId,"美导顾客佣金");
                    money = new BigDecimal("279.93");
                    userService.updateFacePayCommission(money, guideUserId, "20", orderId,"亚健康护理美导师（卖手）");
                }else{
                    money = new BigDecimal("399.9");
                    userService.updateFacePayCommission(money, guideUserId, "20", orderId,"亚健康护理美导师（卖手）");
                }
            }
            // 其他固定人员
            JSONObject fixedCommission = null;
            if(StringUtils.isNotBlank(shopUser.getOfflineCommissionRatio())){
                fixedCommission = JSONObject.parseObject(shopUser.getOfflineCommissionRatio());
                String platform = (String) fixedCommission.get("platform");
                if(StringUtils.isNotBlank(platform)){
                    money = new BigDecimal("79.98");
                    userService.updateFacePayCommission(money, Long.parseLong(platform), "20", orderId,"荣耀兔（平台）");
                }
                String technology = (String) fixedCommission.get("technology");
                if(StringUtils.isNotBlank(technology)){
                    money = new BigDecimal("79.98");
                    userService.updateFacePayCommission(money, Long.parseLong(technology), "20", orderId,"荣耀兔（技术）");
                }
                String devicePartner = (String) fixedCommission.get("devicePartner");
                if(StringUtils.isNotBlank(devicePartner)){
                    money = new BigDecimal("199.95");
                    userService.updateFacePayCommission(money, Long.parseLong(devicePartner), "20", orderId,"设备合伙人");
                }
                String projectOperationCompanyBase = (String) fixedCommission.get("projectOperationCompanyBase");
                if(StringUtils.isNotBlank(projectOperationCompanyBase)){
                    money = new BigDecimal("119.97");
                    userService.updateFacePayCommission(money, Long.parseLong(projectOperationCompanyBase), "20", orderId,"项目承载运营公司（基础保障费用）");
                }
                String healthSeller = (String) fixedCommission.get("healthSeller");
                if(guideUserId == null && StringUtils.isNotBlank(healthSeller)){
                    money = new BigDecimal("399.9");
                    userService.updateFacePayCommission(money, Long.parseLong(healthSeller), "20", orderId,"亚健康护理美导师（卖手）");
                }
                String marketOperationCompany = (String) fixedCommission.get("marketOperationCompany");
                if(StringUtils.isNotBlank(marketOperationCompany)){
                    money = new BigDecimal("239.94");
                    userService.updateFacePayCommission(money, Long.parseLong(marketOperationCompany), "20", orderId,"市场运营公司");
                }
                String attractExecuteCompany = (String) fixedCommission.get("attractExecuteCompany");
                if(StringUtils.isNotBlank(attractExecuteCompany)){
                    money = new BigDecimal("179.955");
                    userService.updateFacePayCommission(money, Long.parseLong(attractExecuteCompany), "20", orderId,"招商执行公司");
                }
                String projectOperationCompany = (String) fixedCommission.get("projectOperationCompany");
                if(StringUtils.isNotBlank(projectOperationCompany)){
                    money = new BigDecimal("379.905");
                    userService.updateFacePayCommission(money, Long.parseLong(projectOperationCompany), "20", orderId,"项目承载运营公司");
                }
                String provinceAgency = (String) fixedCommission.get("provinceAgency");
                if(StringUtils.isNotBlank(provinceAgency)){
                    money = new BigDecimal("39.99");
                    userService.updateFacePayCommission(money, Long.parseLong(provinceAgency),"20", orderId,"省代");
                }
                String cityAgency = (String) fixedCommission.get("cityAgency");
                if(StringUtils.isNotBlank(cityAgency)){
                    money = new BigDecimal("39.99");
                    userService.updateFacePayCommission(money, Long.parseLong(cityAgency), "20", orderId,"市代");
                }
                String areaAgency = (String) fixedCommission.get("areaAgency");
                if(StringUtils.isNotBlank(areaAgency)){
                    money = new BigDecimal("39.99");
                    userService.updateFacePayCommission(money, Long.parseLong(areaAgency), "20", orderId,"区（县）代");
                }
            }
        }else if(facePayStoreOrder.getMoney().compareTo(new BigDecimal("9.9")) == 0
                && facePayStoreOrder.getPayStatus() == 20
                && facePayStoreOrder.getCommissionStatus() == 10 ) {
            // 门店老板
            money = new BigDecimal("4.95");
            userService.updateFacePayCommission(money, facePayStoreOrder.getStoreManagerId(), "20", orderId,"门店老板");
            // 能量舱合伙人
            Set<Long> partnerUserId = getPartnerUserId(storeId);
            for (Long userId1 : partnerUserId) {
                money = new BigDecimal("0.0297");
                userService.updateFacePayCommission(money, userId1, "20", orderId,"能量舱合伙人");
            }
            // 站长
            StorePrincipal storePrincipal = storePrincipalService.findByStoreId(storeId);
            if(storePrincipal != null && storePrincipal.getUserId() != null){
                money = new BigDecimal("0.198");
                userService.updateFacePayCommission(money, storePrincipal.getUserId(), "20", orderId,"亚健康空间站资源方（站长）");
            }
            // 卖手
            if(guideUserId != null){
                if(guideUserPid != null){
                    money = new BigDecimal("0.297");
                    userService.updateFacePayCommission(money, guideUserPid, "20", orderId,"美导顾客佣金");
                    money = new BigDecimal("0.693");
                    userService.updateFacePayCommission(money, guideUserId, "20", orderId,"亚健康护理美导师（卖手）");
                }else{
                    money = new BigDecimal("0.99");
                    userService.updateFacePayCommission(money, guideUserId, "20", orderId,"亚健康护理美导师（卖手）");
                }
            }
            // 其他固定人员
            JSONObject fixedCommission = null;
            if(StringUtils.isNotBlank(shopUser.getOfflineCommissionRatio())){
                fixedCommission = JSONObject.parseObject(shopUser.getOfflineCommissionRatio());
                String platform = (String) fixedCommission.get("platform");
                if(StringUtils.isNotBlank(platform)){
                    money = new BigDecimal("0.198");
                    userService.updateFacePayCommission(money, Long.parseLong(platform), "20", orderId,"荣耀兔（平台）");
                }
                String technology = (String) fixedCommission.get("technology");
                if(StringUtils.isNotBlank(technology)){
                    money = new BigDecimal("0.198");
                    userService.updateFacePayCommission(money, Long.parseLong(technology), "20", orderId,"爱易家（技术）");
                }
                String devicePartner = (String) fixedCommission.get("devicePartner");
                if(StringUtils.isNotBlank(devicePartner)){
                    money = new BigDecimal("0.495");
                    userService.updateFacePayCommission(money, Long.parseLong(devicePartner), "20", orderId,"设备合伙人");
                }
                String projectOperationCompanyBase = (String) fixedCommission.get("projectOperationCompanyBase");
                if(StringUtils.isNotBlank(projectOperationCompanyBase)){
                    money = new BigDecimal("0.297");
                    userService.updateFacePayCommission(money, Long.parseLong(projectOperationCompanyBase), "20", orderId,"项目承载运营公司（基础保障费用）");
                }
                String healthSeller = (String) fixedCommission.get("healthSeller");
                if(guideUserId == null && StringUtils.isNotBlank(healthSeller)){
                    money = new BigDecimal("0.99");
                    userService.updateFacePayCommission(money, Long.parseLong(healthSeller), "20", orderId,"亚健康护理美导师（卖手）");
                }
                String marketOperationCompany = (String) fixedCommission.get("marketOperationCompany");
                if(StringUtils.isNotBlank(marketOperationCompany)){
                    money = new BigDecimal("0.594");
                    userService.updateFacePayCommission(money, Long.parseLong(marketOperationCompany), "20", orderId,"市场运营公司");
                }
                String attractExecuteCompany = (String) fixedCommission.get("attractExecuteCompany");
                if(StringUtils.isNotBlank(attractExecuteCompany)){
                    money = new BigDecimal("0.4455");
                    userService.updateFacePayCommission(money, Long.parseLong(attractExecuteCompany), "20", orderId,"招商执行公司");
                }
                String projectOperationCompany = (String) fixedCommission.get("projectOperationCompany");
                if(StringUtils.isNotBlank(projectOperationCompany)){
                    money = new BigDecimal("0.9405");
                    userService.updateFacePayCommission(money, Long.parseLong(projectOperationCompany), "20", orderId,"项目承载运营公司");
                }
                String provinceAgency = (String) fixedCommission.get("provinceAgency");
                if(StringUtils.isNotBlank(provinceAgency)){
                    money = new BigDecimal("0.099");
                    userService.updateFacePayCommission(money, Long.parseLong(provinceAgency),"20", orderId,"省代");
                }
                String cityAgency = (String) fixedCommission.get("cityAgency");
                if(StringUtils.isNotBlank(cityAgency)){
                    money = new BigDecimal("0.099");
                    userService.updateFacePayCommission(money, Long.parseLong(cityAgency), "20", orderId,"市代");
                }
                String areaAgency = (String) fixedCommission.get("areaAgency");
                if(StringUtils.isNotBlank(areaAgency)){
                    money = new BigDecimal("0.099");
                    userService.updateFacePayCommission(money, Long.parseLong(areaAgency), "20", orderId,"区（县）代");
                }
            }
        }else{
            RedisUtil.del(key);
            return;
        }*/
        facePayStoreOrderService.updateCommissionStatus(orderId,20);
        RedisUtil.del(key);
    }

    @Override
    public List<FacePayCommissionLog> findList(Long userId) {
        return facePayCommissionLogDao.findList(userId);
    }

    public Set<Long> getPartnerUserId(Long storeId){
        Set<Long> partnerUserId = new HashSet<>();
        List<Map<String,Long>>  list = new ArrayList<>();
        Map<String,Long> map = null;
        // 能量舱合伙人
        List<ShoproOrder> shoproOrderList = shoproOrderService.findVerifyList(null,ShoproOrder.goodsId_5000,storeId);
        for(ShoproOrder shoproOrder : shoproOrderList){
            map  = new HashMap<>();
            map.put("time",shoproOrder.getPaytime());
            map.put("userId",shoproOrder.getUserId());
            list.add(map);
        }
        // 能量舱合伙人
//        List<FacePayStoreOrder> facePayStoreOrderList = facePayStoreOrderService.findVerifyList(null,storeId,"5000");
//        for(FacePayStoreOrder facePayStoreOrder : facePayStoreOrderList){
//            if(facePayStoreOrder.getMoney().compareTo(new BigDecimal("5000")) != 0){
//                continue;
//            }
//            map  = new HashMap<>();
//            map.put("time",facePayStoreOrder.getCreatetime());
//            map.put("userId",facePayStoreOrder.getUserId());
//            list.add(map);
//        }
        // 消费合伙人由5000->3000的套餐
        List<FacePayStoreOrder> facePayStoreOrderList = facePayStoreOrderService.findVerifyList(null,storeId,"3000",20,null);
        for(FacePayStoreOrder facePayStoreOrder : facePayStoreOrderList){
            if(facePayStoreOrder.getMoney().compareTo(new BigDecimal("3000")) != 0){
                continue;
            }
            map  = new HashMap<>();
            map.put("time",facePayStoreOrder.getCreatetime());
            map.put("userId",facePayStoreOrder.getUserId());
            list.add(map);
        }
        //  不带参
        list.sort((u1, u2) -> u1.get("time").compareTo(u2.get("time")));
        for(Map<String,Long> map1 : list){
            partnerUserId.add(map1.get("userId"));
            if(partnerUserId.size() >=10){
                return  partnerUserId;
            }
        }
        return partnerUserId;
    }

    public static void main(String[] args) {
        // 线上
        JSONObject onlineCommission = new JSONObject();
        onlineCommission.put("devicePartner","1000147");
        onlineCommission.put("provinceAgency","1000313");
        onlineCommission.put("cityAgency","1000312");
        onlineCommission.put("areaAgency","1000062");
        onlineCommission.put("marketOperationCompany","1000310");
        onlineCommission.put("attractExecuteCompany","1000157");
        System.out.println(onlineCommission.toJSONString());
        // 线下
        JSONObject fixedCommission = new JSONObject();
        fixedCommission.put("platform","1000089");
        fixedCommission.put("technology","1000057");
        fixedCommission.put("devicePartner","1000147");
        fixedCommission.put("projectOperationCompanyBase","1000058");
        fixedCommission.put("healthSeller","1000084");
        fixedCommission.put("marketOperationCompany","1000310");
        fixedCommission.put("attractExecuteCompany","1000157");
        fixedCommission.put("projectOperationCompany","1000058");
        fixedCommission.put("provinceAgency","1000313");
        fixedCommission.put("cityAgency","1000312");
        fixedCommission.put("areaAgency","1000062");
        System.out.println(fixedCommission.toJSONString());
    }

}
